import config from "../util/config";
import {h} from "vue";
import {formatDateTime, isJSON, timePlusZero} from "../util/utils";
import {CommonObject} from "./Common";
import {FormItemRule} from "naive-ui";

export class JobScheduleNameVO {
    id?: bigint;
    scheduleName?: string;
    jobName?: string;
}

export class AddScheduleParam {
    jobMetaDataId?: bigint;
    scheduleName?: string;
    scheduleParam?: string;
    scheduleEnable: boolean = false;
    cronMonth?: number;
    cronDay?: number;
    cronHour?: number;
    cronMinute?: number;
    cronSecond?: number;
}

export class UpdateScheduleParam extends AddScheduleParam{
    id?: bigint;
}

export class JobScheduleVO {
    id?: bigint;
    scheduleName?: string;
    scheduleParam?: string;
    jobCycle?: number;
    scheduleEnable?: boolean;
    lastScheduleTime?: number;
    nextScheduleTime?: number;
    normalNextTime?: number;
    operatorName?: string;
    updateTime?: number;
    cronMonth?: number;
    cronDay?: number;
    cronHour?: number;
    cronMinute?: number;
    cronSecond?: number;
}

export const basicColumns = [
    {
        title: "调度名称",
        key: "scheduleName",
        width: 100,
        ellipsis: {
            tooltip: true
        }
    },
    {
        title: "执行参数",
        key: "scheduleParam",
        width: 100,
        ellipsis: {
            tooltip: true
        }
    },
    {
        title: "执行时间",
        key: "scheduleTime",
        width: 100,
        render: (rowData: JobScheduleVO) => {
            const hour = timePlusZero(rowData.cronHour as number);
            const minute = timePlusZero(rowData.cronMinute as number);
            const second = timePlusZero(rowData.cronSecond as number);
            switch (rowData.jobCycle) {
                case 1:
                    return rowData.cronMonth + "月" + rowData.cronDay + "日" + " " + hour + ":" + minute + ":" + second;
                case 2:
                    return rowData.cronDay + "日" + " " + hour + ":" + minute + ":" + second;
                case 3:
                    let weekDay;
                    switch (rowData.cronDay) {
                        case 1:
                            weekDay = "星期一";
                            break;
                        case 2:
                            weekDay = "星期二";
                            break;
                        case 3:
                            weekDay = "星期三";
                            break;
                        case 4:
                            weekDay = "星期四";
                            break;
                        case 5:
                            weekDay = "星期五";
                            break;
                        case 6:
                            weekDay = "星期六";
                            break;
                        case 7:
                            weekDay = "星期日";
                            break;
                    }
                    return weekDay + " " + hour + ":" + minute + ":" + second;
                case 4:
                    return hour + ":" + minute + ":" + second;
                case 5:
                    return minute + ":" + second;
                case 6:
                    return second + "秒";
                default:
                    return "";
            }
        }
    },
    {
        title: "上次调度时间",
        key: "lastScheduleTime",
        width: config.dateTimeColumnWidth,
        render: (rowData: JobScheduleVO) => {
            if (!rowData.lastScheduleTime) {
                return "-";
            }
            return h("span", formatDateTime(rowData.lastScheduleTime));
        }
    },
    {
        title: "下次调度时间",
        key: "nextScheduleTime",
        width: config.dateTimeColumnWidth,
        render: (rowData: JobScheduleVO) => {
            return h("span", formatDateTime(rowData.nextScheduleTime));
        }
    },
    {
        title: "正常下次调度时间",
        key: "normalNextTime",
        width: config.dateTimeColumnWidth,
        render: (rowData: JobScheduleVO) => {
            return h("span", formatDateTime(rowData.normalNextTime));
        }
    },
    {
        title: "操作人",
        key: "operatorName",
        width: 80
    },
    {
        title: "更新时间",
        key: "updateTime",
        width: config.dateTimeColumnWidth,
        render: (rowData: JobScheduleVO) => {
            return h("span", formatDateTime(rowData.updateTime));
        }
    }
];

export const rules: CommonObject = {
    scheduleName: {
        required: true,
        message: "请输入调度名称",
        trigger: ["blur"]
    },
    scheduleParam: {
        message: "执行参数必须为json格式",
        validator: (_rule: FormItemRule, value: string): boolean => {
            if (!value) {
                return true;
            }
            return isJSON(value);
        },
        trigger: ["blur"]
    }
};
